package com.sg.plugin.calc.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wicket.okrcalc.common.baseinfo.BaseInfoDO;
import com.sg.common.dto.BaseDto;
import com.sg.common.util.CommonFunctionHelper;
import com.wicket.okrcalc.plugin.filter.CustomHttpServletRequestWrapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.core.MethodParameter;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author ：CHS
 * @version 1.0
 * @since 2022/3/29 16:14
 */
public class CommonInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        setUserInfo2Local(request, handlerMethod);

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) throws Exception {
        CommonFunctionHelper.clearBaseInfo();
    }

    private void setUserInfo2Local(HttpServletRequest request, HandlerMethod handlerMethod) {
        try {

            MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
            if (ArrayUtils.isEmpty(methodParameters)) {
                return;
            }
            for (MethodParameter methodParameter : methodParameters) {
                Class clazz = methodParameter.getParameterType();
                //if(ClassUtils.isAssignable(BaseDto.class, clazz)){
                if (request instanceof CustomHttpServletRequestWrapper) {
                    CustomHttpServletRequestWrapper requestWrapper = (CustomHttpServletRequestWrapper) request;
                    String body = requestWrapper.getBody();
                    JSONObject param = JSONObject.parseObject(body);
                    BaseInfoDO baseInfo = new BaseInfoDO();
                    baseInfo.setSpecOriginalRoleMemberId(param.getString("specOriginalRoleMemberId"));
                    baseInfo.setSpecUserId(param.getString("specUserId"));
                    baseInfo.setSpecAppId(param.getString("specAppId"));
                    baseInfo.setSpecSpaceId(param.getString("specSpaceId"));
                    baseInfo.setSpecSceneId(param.getString("specSenceId"));
                    baseInfo.setSpecInductionRecordId(param.getString("specInductionRecordId"));
                    baseInfo.setSpecLoginIp(param.getString("specLoginIp"));
                    baseInfo.setSpecRoleCode(param.getString("specRoleCode"));
                    baseInfo.setSpecSource(param.getString("specSource"));
                    baseInfo.setSpecHighestOrgID(param.getString("specHighestOrgID"));
                    baseInfo.setBusinessData(param.getString("businessData"));
                    if (param.containsKey("localCachePool")) {
                        baseInfo.setLocalCachePool(param.getJSONObject("localCachePool"));
                    }

                    CommonFunctionHelper.setBaseInfoToLocal(baseInfo);

                }
                //}
            }
        } catch (Exception e) {

        }
    }

    private void pushUserInfo2Body(HttpServletRequest request, HandlerMethod handlerMethod) {
        try {

            MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
            if (ArrayUtils.isEmpty(methodParameters)) {
                return;
            }
            for (MethodParameter methodParameter : methodParameters) {
                Class clazz = methodParameter.getParameterType();
                if (ClassUtils.isAssignable(BaseDto.class, clazz)) {
                    if (request instanceof CustomHttpServletRequestWrapper) {
                        CustomHttpServletRequestWrapper requestWrapper = (CustomHttpServletRequestWrapper) request;
                        String body = requestWrapper.getBody();
                        JSONObject param = JSONObject.parseObject(body);
                        requestWrapper.setBody(JSON.toJSONString(param));
                    }
                }
            }
        } catch (Exception e) {

        }
    }
}

